#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <hy_log/hy_log.h>

#include <hy_os/hy_mem.h>

#define ser_ip "192.168.10.100"   //改为对方的ip
unsigned short portnum = 7000;

#define _BUF_LEN_MXA (1024)

int main(int argc, char **argv)
{
    int cfd;
    int recbyte;
    int sin_size;
    char buffer[_BUF_LEN_MXA + 1];
    struct sockaddr_in s_add;
    char data[_BUF_LEN_MXA];

    HyLogConfig_s log_c;
    HY_MEMSET(&log_c, sizeof(HyLogConfig_s));
    log_c.port                      = 0;
    log_c.fifo_len                  = 10 * 1024;
    log_c.config_file               = "../res/hy_log/zlog.conf";
    log_c.save_c.level              = HY_LOG_LEVEL_INFO;
    log_c.save_c.output_format      = HY_LOG_OUTFORMAT_ALL;

    HyLogInit(&log_c);

    memset(data,'A',_BUF_LEN_MXA);
    printf("Hello,welcome to client!\r\n");

    cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == cfd) {
        printf("socket fail ! \r\n");
        return -1;
    }

    bzero(&s_add,sizeof(struct sockaddr_in));
    s_add.sin_family=AF_INET;
    s_add.sin_addr.s_addr= inet_addr(ser_ip);
    s_add.sin_port=htons(portnum);
    //printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port);

    if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr))) {
        printf("connect fail !\r\n");
        return -1;
    }

    if(send(cfd,data,_BUF_LEN_MXA,0)<0) {
        perror("send fail:");
    }

    while(1) {
        if(-1 == (recbyte = recv(cfd, buffer, _BUF_LEN_MXA,0))) {
            printf("read data fail !\r\n");
            return -1;
        }
        buffer[recbyte]='\0';

        LOGI("recbyte: %d\n%s \n", recbyte, buffer);
    }
    close(cfd);
    return 0;
}
